﻿@{
    ViewData["Title"] = "Test";
    Layout = "~/Views/Shared/_LayuiMain.cshtml";
}
@section head{
<link href="~/layuiadmin/layui/extend/eleTree.css" rel="stylesheet" />
}
<button type="Button" class="layui-btn layui-btn-danger" onclick="setChecked()">设置</button>
<button type="Button" class="layui-btn layui-btn-danger" onclick="getChecked()">获取</button>
<div class="eleTree ele1" lay-filter="data1"></div>
<script>
    layui.config({
        base: '../layuiadmin/layui/extend/' //静态资源所在路径
    });
    addLayuiApp("eleTree");
    var el1
    addLayuiUseEvent(function () {
        var eleTree = layui.eleTree;

        el1 = eleTree.render({
            elem: '.ele1',
            //data: data,
            url: "/api/apirole/GetMenuTree",
            method: "post",
            showCheckbox: true,
            renderAfterExpand: true,
            expandOnClickNode: true,
            defaultExpandAll: true,
            //checkStrictly: true,
            //autoExpandParent:false,
            request: {
                name: "title",
                key: "id",
                children: "children",
                checked: "checked",
                disabled: "disabled"
                //isLeaf: "spread"
            },
            done: function (res) {
                el1.setChecked(['000101','000103'],true);
            }
            //defaultCheckedKeys: [23]
        });
        console.log(eleTree.config);
    });
    function setChecked() {
        
        el1.setChecked(selectedIds,true);
    }

    var selectedIds = [];
    var selectedAllIds = [];
    function getChecked() {
        selectedIds.splice(0, selectedIds.length);
        selectedAllIds.splice(0, selectedAllIds.length);
        var nodes = el1.getChecked(false, true);
        $.each(nodes, function (index, val) {
            selectedAllIds.push(val.id);
        });

        var nodes2 = el1.getChecked(true, false);
        $.each(nodes2, function (index, val) {
            selectedIds.push(val.id);
        });
        console.log("selectedAllIds");
        console.log(selectedAllIds);
        console.log("selectedIds");
        console.log(selectedIds);
    }
</script>
